home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 122_01 / pist2a.c < prev    next >
Text File  |  1984-03-05  |  6KB  |  276 lines

  1. /*********************************************************/
  2. /*                             */
  3. /* PISTOL-Portably Implemented Stack Oriented Language     */
  4. /*            Version 2.0             */
  5. /* (C) 1983 by    Ernest E. Bergmann             */
  6. /*        Physics, Building #16             */
  7. /*        Lehigh Univerisity             */
  8. /*        Bethlehem, Pa. 18015             */
  9. /*                             */
  10. /* Permission is hereby granted for all reproduction and */
  11. /* distribution of this material provided this notice is */
  12. /* included.                         */
  13. /*                             */
  14. /*********************************************************/
  15. /* second module for PISTOL v2.0 in BDS 'C', v1.45a */
  16. /* August 30, 1982 */
  17. #include "bdscio.h"
  18. #include "pistol.h"
  19.  
  20. ttyi()    /* inputs character from keyboard, buffered by line */
  21. {    /* static char keycurs,keylen,keystring[MAXORD]; */
  22.     if(keycurs>keylen)
  23.         {keylen=strlen(gets(keystring));
  24.         if(ram[8].in)
  25.             {fputs(keystring,list);
  26.             fputs("\n",list);
  27.             }
  28.         keycurs=0;
  29.         keystring[keylen]=NEWLINE;
  30.         }
  31.     push(keystring[keycurs++]);
  32. }
  33.  
  34. pop()
  35. {    if(stkptr<1) merr(undflo);
  36.     else return(stack[stkptr--]);
  37. }
  38.  
  39. carret()
  40. {    if(ram[10].in)
  41.         {if(ram[17].in==++ram[18].in)
  42.             {ttyi();ram[18].in=0;
  43.             if('Q'==toupper(pop()) ) abort();
  44.             }
  45.         ram[20].in=0;
  46.         printf("\n");
  47.         }
  48.     if(ram[8].in) fprintf(list,"\n");
  49. }
  50.  
  51. spaces(num)
  52. char num;
  53. {    while(num) { chout(' '); num-- ; }
  54. }
  55.  
  56. tab()
  57. {    if(ram[23].cr)
  58.         spaces(ram[23].cr-ram[20].in%ram[23].cr);
  59. }
  60.  
  61. chout(ch)
  62. char ch;
  63. {    if(ch==NEWLINE) carret();
  64.     else if (ch==9) tab();
  65.     else {    if(ram[20].in==ram[19].in) carret();
  66.         ram[20].in++;
  67.         if(ram[10].in) putc(ch,1);
  68.         if(ram[8].in)putc(ch,list);
  69.         }
  70. }
  71.  
  72. message(st)
  73. char *st;
  74. {char *last;
  75. char len;
  76.     len=*st;
  77.     last=st + *st;
  78.     while(st < last){st++; chout(*st);}
  79. }
  80.  
  81.  
  82. interpret()
  83. {    do    {ip += W;
  84.         if(instr<NFUNCS) (*farray[instr])();
  85.         else  {if((instr<&ram)||(instr>&ram[RAMSIZE]))
  86.                 merr(readv);
  87.             rpush(ip); ip=instr;
  88.             }
  89.         Pw = ip;
  90.         instr=*Pw;
  91.         /* trace patch here */
  92.         if(rptr==(ram[15].in-2))
  93.             {savinstr=instr;
  94.             savlevel=rptr;
  95.             instr=ram[22].in;
  96.             ip -= W;
  97.             do    {ip += W;
  98.                 if(instr<NFUNCS)
  99.                     (*farray[instr])();
  100.                 else    {
  101.                     if((instr<&ram)||
  102.                     (instr>&ram[RAMSIZE]))
  103.                         merr(readv);
  104.                     rpush(ip); ip=instr;
  105.                     }
  106.                 Pw=ip; instr=*Pw;
  107.                 }
  108.             while( rptr > savlevel);
  109.             instr=savinstr;
  110.             }
  111.         }
  112.     while (rptr >= 0);
  113.     ip -= W;
  114. }
  115.  
  116. merr(m)
  117. char *m;
  118. {    ram[10].in=TRU;
  119.     if(ram[20].in) carret();
  120.     message(m);
  121.     abort();
  122. }
  123.  
  124. synterr()
  125. {ram[10].in=TRU;if(ram[20].in)carret();
  126.     if((ram[7].in) && (ram[9].in==FALS))
  127.         message(&strings[LINEBUF]);
  128.     merr(synt);
  129. }
  130.  
  131. push(item)
  132. int item;
  133. {    if(++stkptr >= SSIZE) merr(ovflo);
  134.     stack[stkptr]=item;
  135. }
  136.  
  137. rpush(item)
  138. int item;
  139. {    if(++rptr >= RSIZE) merr(ovflo);
  140.     rstack[rptr]=item;
  141. }
  142.  
  143.  
  144. lpush(item)
  145. int item;
  146. {if(LSIZE<= ++lptr) merr(ovflo); lstack[lptr]=item;}
  147.  
  148. cpush(item)
  149. int item;
  150. {if(CSIZE<= ++cptr) merr(ovflo); cstack[cptr]=item; }
  151.  
  152.  
  153. pushck(chkch)
  154. char chkch;
  155. { if(CHKLMT>(++strings[1])) strings[1+strings[1]]=chkch;
  156. else{ram[10].in=TRU; message(ovflo); synterr();}
  157. }
  158.  
  159. append(item)    /* place item at end of dictionary */
  160. int    item;
  161. {
  162.     (*ram[2].pw).in=item;
  163.     ram[2].pw++;
  164.     if( ram[2].pw>=&ram[COMPBUF] ) merr(writv);
  165. }
  166.  
  167. aloop()
  168. {if(lstack[lptr]<lstack[lptr-1]){Pw=ip;ip += *Pw; }
  169. else{lptr -=3; if(lptr<0) merr(undflo); ip +=W ; }
  170. }
  171.  
  172. pdo()
  173. {/* static int start,nd; */
  174.     start=pop(); nd=pop();
  175.     if(start<nd)
  176.         {lpush(start);
  177.         lpush(nd);
  178.         lpush(start);/* init iteration var.*/
  179.         ip += W;
  180.         }
  181.     else    {Pw=ip; ip += *Pw ; }
  182. }
  183.  
  184.  
  185. dropck()
  186. {if(strings[1]) strings[1]--; else synterr(); }
  187.  
  188.  
  189. int *vfind()/*??? cleanup variable names and static*/
  190. {char *str2;
  191. /*ptoken,vloc,vmatch,prev,pcount,*Pc3 are "static"*/
  192.     vloc=pop(); prev=vloc=*vloc;
  193.     ptoken=pop();
  194.     if((ptoken<&strings)||(ptoken>=&strings[LINEBUF]))
  195.         merr(readv);
  196.     if(vloc) do
  197.     {vmatch=TRU;
  198.     if(Pw=*(vloc-2),*Pw==*ptoken)
  199.         for(Pc3=Pw,vmatch=*Pc3,str2=ptoken;
  200.         vmatch&&(*++Pc3 == *++str2) ; --vmatch);
  201.     if(vmatch)
  202.         {prev=vloc;vloc=*(vloc-3);}
  203.     }
  204.     while(vloc && vmatch);
  205.     push(vloc);
  206.     ram[6].pw=prev;
  207. }
  208.  
  209. find()
  210. {int *ptoken,*loc,*v;
  211.     ptoken=pop();
  212.     for(loc=FALS,v=ram[28].pw;(v>=&ram[VBASE])&& !loc; v--)
  213.         {push(ptoken);push(*v);vfind();loc=pop();}
  214.     push(loc);
  215. }
  216.  
  217. enter()
  218. {int ptoken;
  219.     ptoken=stack[stkptr];
  220.     find();
  221.     if(pop())
  222.         {message(redef);
  223.         spaces(3);
  224.         message(ptoken);
  225.         carret();
  226.         }
  227.     append(0);
  228.     append((*ram[5].pw).in);
  229.     append(ptoken);
  230.     append(COMPHERE);
  231.     (*ram[5].pw).in=ram[2].in;
  232. }
  233.  
  234. fenter()
  235. {Pw=(*ram[5].pw).pw-4; *Pw=pop(); }
  236.  
  237. geoln()
  238. {ram[11].pc=&strings[LINEBUF]+strings[LINEBUF];}
  239.  
  240. getline()
  241. {if(ram[7].in) finline(ldfil);
  242. else cinline();
  243. if(ram[9].in&&ram[7].in) message(&strings[LINEBUF]);
  244. }
  245.  
  246.  
  247. cinline()    /*input line from console*/
  248. {    keycurs=1+keylen; /* for emptying ttyi() buffer */
  249.     ram[11].pc=&strings[LINEBUF+1];
  250.     ram[12].in=1+strlen(gets(&strings[LINEBUF+1]));
  251.     Pc=&strings[LINEBUF];
  252.     *Pc=ram[12].in;
  253.     Pc += ram[12].in;
  254.     *Pc=NEWLINE; Pc++ ;
  255.     *Pc = 0;/* for 'C' string terminator*/
  256.     if(ram[8].in)fputs(ram[11].pc,list);
  257. }
  258.  
  259. finline(iobuf)
  260. char *iobuf;
  261. {    ram[11].pc=fgets(&strings[LINEBUF+1],iobuf);
  262.     if(!ram[11].in) merr(feof);
  263.     ram[12].in=strlen(ram[11].pc);
  264.     Pc=&strings[LINEBUF];
  265.     *Pc=ram[12].in;
  266.     Pc += ram[12].in;
  267.     *Pc=NEWLINE; Pc++ ;
  268.     *Pc=0 ; /*???? should echo to list? */
  269.     if(ram[8].in&&ram[9].in)fputs(ram[11].pc,list);
  270. }
  271.  
  272. ret();
  273.         }
  274.     append(0);
  275.     append((*ram[5].pw).in);
  276.     app